package pta.test0809.继承覆盖综合练习;


import java.text.DecimalFormat;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<Person> personList = new ArrayList<>();
        List<Student> stuList = new ArrayList<>();
        List<Employee> empList = new ArrayList<>();
        boolean flag = true;
        while (true){
            String str = scan.nextLine();
            String[] strings = cut(str," ");
            if (!strings[0].equals("exit")){
                if (flag){
                    switch (strings[0]){
                        case "s":
                            personList.add(new Student(strings[1],Integer.parseInt(strings[2]),Boolean.parseBoolean(strings[3]),strings[4],strings[5]));
                            break;
                        case "e":
                            personList.add(new Employee(strings[1],Integer.parseInt(strings[2]),Boolean.parseBoolean(strings[3]),
                                    Double.parseDouble(strings[4]),strings[5] == "null"? null: new Company(strings[5])));
                            break;
                        default:
                            flag = false;
                            //排序
                            personList.sort(new Comparator<Person>() {
                                @Override
                                public int compare(Person o1, Person o2) {
                                    int sort = o1.name.compareTo(o2.name);
                                    if(sort == 0) return o1.age - o2.age;
                                    return sort;
                                }
                            });
                            //分裝
                            for (int i=0;i<personList.size();i++){
                                Person person = personList.get(i);
                                if (person instanceof Student) {
                                    if (stuList.indexOf(person) == -1) stuList.add((Student) person);
                                } else {
                                    if (empList.indexOf(person) == -1)  empList.add((Employee) person);
                                }
                            }
                            //輸出
                            for(int i=0;i<personList.size();i++){
                                System.out.println(personList.get(i));
                            }

                    }
                } else {
                    System.out.println("stuList");
                    for(Person p:stuList) System.out.println(p);
                    System.out.println("empList");
                    for(Person p:empList) System.out.println(p);
                }
            }else return;
        }
    }
    private static String[] cut(String str, String s) {
        String[]  strings = str.split(s);
        for (int i=0;i<strings.length;i++){
            if (strings[i].equals("null")) strings[i] = null;
        }
        return strings;
    }
}

abstract class Person{
    String name;
    int age;
    boolean gender;

    public Person(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return this.name + "-" + this.age + "-" + this.gender;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        Person that = (Person) obj;
        return (this.name == that.name|| this.name != null && this.name.equals(that.name))
                && this.age == that.age && this.gender == that.gender;
    }
}

class Student extends Person{
    private String stuNo;
    private String clazz;

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public Student(String name, int age, boolean gender, String stuNo, String clazz) {
        super(name, age, gender);
        this.setStuNo(stuNo);
        this.setClazz(clazz);
    }

    @Override
    public String toString() {
        return "Student:" + super.toString() + "-" + this.getStuNo() + "-" + this.getClazz();
    }
    @Override
    public boolean equals(Object obj){
        if (super.equals(obj)){
            Student that = (Student) obj;
            if((that.stuNo == this.stuNo || that.stuNo != null && that.stuNo.equals(this.stuNo))
                    && (that.clazz == this.clazz || that.clazz != null && that.clazz.equals(this.clazz)))
                return true;
        }
        return false;
    }
}

class Company{
    public String name;

    public Company(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Company company = (Company) obj;
        if(company.name == this.name || company.name != null && company.name.equals(this.name))
            return true;

        return false;
    }
}

class Employee extends Person{
    private Company company;
    private double salary;

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee(String name, int age, boolean gender,double salary,Company company) {
        super(name, age, gender);
        this.setCompany(company);
        this.setSalary(salary);
    }

    @Override
    public String toString() {
        return "Employee:" + super.toString() + "-" + this.getCompany() +"-" + this.getSalary();
    }

    @Override
    public boolean equals(Object obj) {
        if (super.equals(obj)){
            Employee that = (Employee)obj;
            DecimalFormat df = new DecimalFormat("#.#");
            return (this.company == that.company||this.company != null && this.company.equals(that.company))
                    && df.format(this.salary).equals(df.format(that.salary));
        }
        return false;
    }
}